<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="16">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('结账单号')">
              <a-input v-model="queryParam.id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('结账时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
                style="min-width: calc(50% - 10px)"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
                style="min-width: calc(50% - 10px)"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('设备名称(编码)')">
              <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                  <span>{{ item.name }}</span>
                  <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="5" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('设备类型')">
                <a-select v-model="queryParam.deviceType">
                  <a-select-option :value="item.value" :key="index" v-for="(item, index) in devicetypeList"
                    >{{ item.title }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('操作员')">
                <a-select v-model="queryParam.operator">
                  <a-select-option v-for="(item, index) in userList" :value="item.id" :key="index">{{
                    item.realname
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('结账人')">
                <a-select v-model="queryParam.createBy">
                  <a-select-option v-for="(item, index) in userList" :value="item.id" :key="index">{{
                    item.realname
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('结账类型')">
                <a-select v-model="queryParam.checkType">
                  <a-select-option value="">{{ $t('全部') }}</a-select-option>
                  <a-select-option value="0">{{ $t('人工结账') }}</a-select-option>
                  <a-select-option value="1">{{ $t('自动结账') }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div class="table-operator">
      <a-button v-has="'BisCheckOutController:export'" type="primary" icon="download" :loading="exloading" @click="handleExportXls($t('设备结账记录'))">{{
        $t('导出')
      }}</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">{{ $t('查看账单') }}</a>
        </span>
        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('现金应收：') }}</span
            ><span>{{
              totalInfo.shouldPay || totalInfo.shouldPay === 0 || totalInfo.shouldPay === '0'
                ? totalInfo.shouldPay.toFixed(2)
                : '0.00'
            }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('现金实收：') }}</span
            ><span>{{
              totalInfo.actualPay || totalInfo.actualPay === 0 || totalInfo.actualPay === '0'
                ? totalInfo.actualPay.toFixed(2)
                : '0.00'
            }}</span>
          </div>
        </template>
      </a-table>
    </div>

    <check-out-detailt-modal ref="modalForm" @ok="modalFormOk"></check-out-detailt-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import CheckOutDetailtModal from './modules/CheckOutDetailtModal'

export default {
  name: 'EquipmentCheckOut',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    CheckOutDetailtModal,
  },
  data() {
    return {
      moment,
      description: '设备结账记录',
      totalShould: 0,
      totalActualPay: 0,
      // 表头
      columns: [
        {
          title: this.$t('结账单号'),
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: this.$t('结账类型'),
          align: 'center',
          dataIndex: 'checkType_dictText',
        },
        {
          title: this.$t('设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
        },
        {
          title: this.$t('设备名称'),
          align: 'center',
          dataIndex: 'deviceName',
        },
        {
          title: this.$t('设备类型'),
          align: 'center',
          dataIndex: 'deviceType_dictText',
        },
        {
          title: this.$t('操作员'),
          align: 'center',
          dataIndex: 'operator',
        },
        {
          title: this.$t('账目开始时间'),
          align: 'center',
          dataIndex: 'startTime',
        },
        {
          title: this.$t('结账时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('结账人'),
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: this.$t('总应收'),
          align: 'center',
          dataIndex: 'shouldPay',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function (col) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('总实收'),
          align: 'center',
          dataIndex: 'actualPay',
          customRender: function (text) {
            return text.toFixed(2)
          },
          customCell: function (col) {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/checkout/bisCheckOut/list',
        delete: '/checkout/bisCheckOut/delete',
        deleteBatch: '/checkout/bisCheckOut/deleteBatch',
        exportXlsUrl: '/checkout/bisCheckOut/exportXls',
        importExcelUrl: 'checkout/bisCheckOut/importExcel',
        totalUrl: '/checkout/bisCheckOut/total',
      },
      dictOptions: {},
      deviceList: [],
      userList: [],
      devicetypeList: [],
      queryParam: {
        deviceId: '',
        operator: '',
        createBy: '',
        checkType: '',
        deviceType: ''
      },
      isfooter: true,
      totalInfo: {
        shouldPay: 0,
        actualPay: 0,
      },
      isCustom: true,
      isHideData: false,
    }
  },
  created() {},
  mounted() {
    let that = this
    document.onkeydown = function (e) {
      let key = window.event.keyCode
      if (key == 75 && event.ctrlKey) {
        window.event.preventDefault()
        that.$refs.modalForm.visible = false
        that.isHideData = false
        that.queryParam.dataType = 0
        that.searchQuery()
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      getAction('/sys/user/list').then((res) => {
        if (res.success) {
          this.userList = res.result.records
          this.userList.forEach((col) => {
            col.id = col.realname
          })
          this.userList.unshift({ id: '', realname: this.$t('全部') })
        }
      })
      initDictOptions('bis_device_info.type').then((res) => {
        if (res.success) {
          this.devicetypeList = res.result
          this.devicetypeList.unshift({ title: this.$t('全部'), value: '' })
        }
      })
      this.customInit()
    },
    customInit() {
      this.handleSearch('')
      this.isHideData = true
      this.queryParam.dataType = 1
      this.loadData(1)
    },
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    searchReset() {
      this.handleSearch('')
      this.queryParam = {
        deviceId: '',
        operator: '',
        createBy: '',
      }
      this.customInit()
    },
    handleDetail(record) {
      let dtl = Object.assign({}, record, { dataType: this.queryParam.dataType })
      this.$refs.modalForm.edit(dtl)
      this.$refs.modalForm.title = this.$t('详情')
      this.$refs.modalForm.disableSubmit = true
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>